
#include "fsmv2.h"

#include <stdio.h>
// #define ALOGD printf

namespace serial {

int FsmV2::onData(unsigned char data)
{
    // ALOGD("v2: on data %#02x, now state: %d\n", data & 0xff, mState);
    switch (mState) {
    case S_INIT:
        return (data == 0xdd) ? changeState(S_HEADERDD) : fail("not dd");
    case S_HEADERDD:
        return (data == 0xff) ? changeState(S_HEADERFF) : fail("not ff");
    case S_HEADERFF:
        return changeState(S_BOARDID);
    case S_BOARDID:
        return (data == 0x04) ? changeState(S_LENGTH) : fail("not 04");
    case S_LENGTH:
        return (data == 0xa1) ? changeState(S_A1) : fail("not a1");
    case S_A1:
        return changeState(S_CMD1);
    case S_CMD1:
        return changeState(S_CMD2);
    case S_CMD2:
        return changeState(S_DATA);
    case S_DATA:
        return (data == 0xbb) ? changeState(S_ENDBB) : fail("not bb");
    case S_ENDBB:
        return (data == 0xcc) ? 1 : fail("not cc");
    default:
        return -1;
    }
    return 0;
}

void FsmV2::reset()
{
    mState = S_INIT;
}

int FsmV2::changeState(State s)
{
    mState = s;
    return 0;
}

int FsmV2::fail(const char * reason)
{
    // ALOGD("not v2: %s\n", reason);
    return -1;
}
} // namespace serial

